﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class UserControlSearchPayOut : UserControl, InterfaceSearchControl
    {
        private bool _isNotFill = true;

        private bool _openOnDblClick = false;

        public UserControlSearchPayOut()
        {
            InitializeComponent();

            radGridViewList.MultiSelect = true;
        }

        #region InterfaceSearchControl Members

        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewList.EnableFiltering;
            }
            set
            {
                radGridViewList.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                DataRow dr;
                if (radGridViewList.SelectedRows.Count > 0)
                {
                    dr = (((DataRowView)(radGridViewList.SelectedRows.First()).DataBoundItem)).Row;
                }
                else
                {
                    dr = null;
                }
                return dr;
            }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get
            {
                return radGridViewList.SelectedRows;
            }
        }

        public DataSet OwnerDataSet
        {
            get { return dataSetFSO1; }
            set { _isNotFill = false; dataSetFSO1 = (DataSetFSO)value; InitializeGridView(); }
        }

        public void RefreshList()
        {
            paymenT_OUTTableAdapter1.Fill(dataSetFSO1.PAYMENT_OUT);
            this.InitializeGridView();
        }
        #endregion

        private void UserControlSearchPayOut_Load(object sender, EventArgs e)
        {
            if (_isNotFill)
            {
                paymenT_OUTTableAdapter1.Fill(dataSetFSO1.PAYMENT_OUT);             
            }
            this.InitializeGridView();
        }

        private void InitializeGridView()
        {
            GridViewTextBoxColumn colNumber = new GridViewTextBoxColumn("NUMBER", dataSetFSO1.PAYMENT_OUT.NUMBERColumn.ColumnName);
            colNumber.HeaderText = "№";

            GridViewDateTimeColumn colDate = new GridViewDateTimeColumn("DATE", dataSetFSO1.PAYMENT_OUT.DATEColumn.ColumnName);
            colDate.HeaderText = "Дата";
            colDate.FormatString = "{0:d}";

            GridViewDecimalColumn colSumCur = new GridViewDecimalColumn("SUM", dataSetFSO1.PAYMENT_OUT.SUM_RUBColumn.ColumnName);
            colSumCur.HeaderText = "Сумма";
            colSumCur.Minimum = 0;
            colSumCur.FormatString = "{0:N2} Руб.";

            radGridViewList.MasterTemplate.Columns.Clear();
            radGridViewList.MasterTemplate.Columns.Add(colNumber);
            radGridViewList.MasterTemplate.Columns.Add(colDate);
            radGridViewList.MasterTemplate.Columns.Add(colSumCur);

            radGridViewList.DataSource = dataSetFSO1.PAYMENT_OUT;

        }

        private void radGridViewList_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            if (this.SelectedRow != null && _openOnDblClick)
            {
                FormPaymentOut formPayOut = new FormPaymentOut((DataSetFSO.PAYMENT_OUTRow)this.SelectedRow);
                if (!FormHelper.HasOpenedForm(formPayOut.FormName))
                {
                    formPayOut.MdiParent = FormHelper.MainForm;
                    formPayOut.Show();
                    formPayOut.Select();
                }
            }
        }
    }
}
